<!DOCTYPE HTML>
<html lang="en">
<head>
<title>If (Expression) - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The &quot;if (expression)&quot; statement specifies one or more statements to execute if an expression evaluates to true." />
<meta name="ahk:equiv-v2" content="commands/If.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>If (Expression)</h1>
<p>Specifies one or more <a href="../Concepts.htm#statement">statements</a> to execute if an <a href="../Variables.htm#Expressions">expression</a> evaluates to true.</p>
<pre class="Syntax"><span class="func">If</span> (<i>Expression</i>)
{
    <i>Statements</i>
}</pre>

<h2>Remarks</h2>
<p>An <em>If</em> statement that contains an expression is usually differentiated from a <a href="IfEqual.htm">traditional If statement</a> such as <code>if FoundColor != Blue</code> by enclosing the expression in parentheses, as in <code>if (FoundColor != "Blue")</code>. However, this is not strictly required, as any <em>If</em> statement which does not match any of the <a href="../Language.htm#legacy-if">legacy if patterns</a> is assumed to contain an expression. In particular, the following are also common ways of writing an <em>If (expression)</em>:</p>
<ul>
  <li>Starting with an open parenthesis but not fully enclosing the expression: <code>if (x &gt; 0) and (y &gt; 0)</code></li>
  <li>Starting with a <a href="../Functions.htm">function call</a>: <code>if InStr(a, b)</code></li>
  <li>Starting with an <a href="../Variables.htm#Operators">operator</a> such as <code>not</code> or <code>!</code>: <code>if !MyVar</code></li>
</ul>
<p><strong>Known limitation:</strong> For historical reasons, <em>If (expression)</em> actually accepts a <a href="../Language.htm#numeric-parameters">numeric parameter</a> rather than a pure expression. For example, <code>if %MyVar%</code> is equivalent to <code>if MyVar</code>. This can be avoided by always enclosing the expression in parentheses.</p>
<p>If the <em>If</em> statement's expression evaluates to true (which is any result other than an empty string or the number 0), the line or <a href="Block.htm">block</a> underneath it is executed. Otherwise, if there is a corresponding <a href="Else.htm">Else</a> statement, execution jumps to the line or block underneath it.</p>
<p>If an <em>If</em> owns more than one line, those lines must be enclosed in braces (to create a <a href="Block.htm">block</a>). However, if only one line belongs to an <em>If</em>, the braces are optional. See the examples at the bottom of this page.</p>
<p>The space after <code>if</code> is optional if the expression starts with an open-parenthesis, as in <code>if(expression)</code>.</p>
<p id="otb">The One True Brace (OTB) style may optionally be used with <em>If</em> statements that are expressions (but not <a href="IfEqual.htm">traditional If statements</a>). For example:</p>
<pre>if (x &lt; y) {
    <em>; ...</em>
}
if WinExist(&quot;Untitled - Notepad&quot;) {
    WinActivate
}
if IsDone {
    <em>; ...</em>
} else {
    <em>; ...</em>
}</pre>
<p>Unlike an <em>If</em> statement, an <a href="Else.htm">Else</a> statement supports any type of statement immediately to its right.</p>
<p>On a related note, the statement <code><a href="IfBetween.htm">if Var between LowerBound and UpperBound</a></code> checks whether a variable is between two values, and <code><a href="IfIn.htm">if Var in MatchList</a></code> can be used to check whether a variable's contents exist within a list of values.</p>

<h2>Related</h2>
<p><a href="../Variables.htm#Expressions">Expressions</a>, <a href="SetExpression.htm">Assign expression (:=)</a>, <a href="IfIn.htm">if var in/contains</a>, <a href="IfBetween.htm">if var between</a>, <a href="IfInString.htm">IfInString</a>, <a href="Block.htm">Blocks</a>, <a href="Else.htm">Else</a>, <a href="While.htm">While-loop</a></p>

<h2>Examples</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>if (A_Index &gt; 100)
    return

if (A_TickCount - StartTime &gt; 2*MaxTime + 100)
{
    MsgBox Too much time has passed.
    ExitApp
}

if (Color = &quot;Blue&quot; or Color = &quot;White&quot;)
{
    MsgBox The color is one of the allowed values.
    ExitApp
}
else if (Color = &quot;Silver&quot;)
{
    MsgBox Silver is not an allowed color.
    return
}
else
{
    MsgBox This color is not recognized.
    ExitApp
}</pre>
</div>

<div class="ex" id="ExMultiStatement">
<p><a href="#ExMultiStatement">#2</a>: A single <a href="../Variables.htm#comma">multi-statement</a> line does not need to be enclosed in braces.</p>
<pre>MyVar := 3
if (MyVar &gt; 2)
    MyVar++, MyVar := MyVar - 4, MyVar .= " test"
MsgBox % MyVar  <em>; Shows "0 test".</em>
</pre>
</div>

</body>
</html>
